CREATE OR REPLACE FUNCTION delete_dimensao_chaveFato() RETURNS trigger AS '
BEGIN
  IF tg_op = ''DELETE'' THEN
     DELETE FROM DIM_CHAVEFATO WHERE ID=OLD.DIM_CHAVEFATO;
  END IF;
  RETURN old;
END
' LANGUAGE plpgsql;

CREATE TRIGGER delete_fato AFTER DELETE ON fato
  FOR EACH ROW EXECUTE PROCEDURE delete_dimensao_chaveFato();


CREATE OR REPLACE FUNCTION delete_dimensao_chaveLancamentos() RETURNS trigger AS '
BEGIN
  IF tg_op = ''DELETE'' THEN
     DELETE FROM DIM_CHAVELANCAMENTOS WHERE ID=OLD.DIM_CHAVELANCAMENTO;
  END IF;
  RETURN old;
END
' LANGUAGE plpgsql;

CREATE TRIGGER delete_lancamentos AFTER DELETE ON lancamentos
  FOR EACH ROW EXECUTE PROCEDURE delete_dimensao_chaveLancamentos();
